package middleware

import (
	"github.com/chenjiandongx/ginprom"
	"github.com/gin-gonic/gin"
	"github.com/prometheus/client_golang/prometheus/promhttp"

	"github.com/super-team/start-kit/middlewares/cors"
	"github.com/super-team/start-kit/middlewares/logger"
	rate_limit "github.com/super-team/start-kit/middlewares/rate-limit"
	"github.com/super-team/start-kit/middlewares/secure"
)

func RegisterMiddleware(router *gin.Engine) {

	// 性能监控
	router.Use(ginprom.PromMiddleware(nil))
	//性能监控
	router.GET("/metrics", ginprom.PromHandler(promhttp.Handler()))

	middlewares := []gin.HandlerFunc{
		//panic恢复
		gin.Recovery(),
		//请求日志
		logger.SetLogger(logger.Config{
			SkipPath: getSkipPath(),
		}),
		//cors跨域
		cors.CORS(),
		//安全策略
		secure.Secure(),
		//请求频率限制
		rate_limit.RateLimit(),
	}
	router.Use(middlewares...)
}

//获取不需要记录请求日志的路由
func getSkipPath() []string {
	return []string{"/api/login"}
}
